/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */

/**
 * @param {ListNode} headA
 * @param {ListNode} headB
 * @return {ListNode}
 */
var getIntersectionNode = function (headA, headB) {
  // 判空
	// 两个链表中只要有一个为空，就返回空
	if (!headA || !headB) {
		return null;
	}

	// 计算两个链表长度的差值
	let node = headA;
	let length = 0;
	while (node) {
		length++;
		node = node.next;
	}
	node = headB;
	while (node) {
		length--;
		node = node.next;
	}
	// 使fast指向长链表
	let diff = length;
	let fast = headA, slow = headB;
	if (length<0) {
		fast = headB;
		slow = headA;
		diff = -length;
	}
	// 长链表先走
	for (let i = 0; i < diff; i++) {
		fast = fast.next;
	}
	// 两个链表一起遍历
	while (fast !== slow) {
		fast = fast.next;
		slow = slow.next;
	}
	return fast; 
};